<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>角色列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--模糊搜索区域-->
	<div class="layui-row">
		<form class="layui-form layui-col-md12 ok-search">
			<input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="startTime">
			<input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="endTime">
			<input class="layui-input" placeholder="销售描述" autocomplete="off" name="description">
			<button class="layui-btn" lay-submit="" lay-filter="search">
				<i class="layui-icon layui-icon-search"></i>
			</button>
		</form>
	</div>
	<!--数据表格-->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
	layui.use(["element", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;
		let okLayer = layui.okLayer;
		let okUtils = layui.okUtils;
		let okMock = layui.okMock;
		okLoading.close();
		laydate.render({elem: "#startTime", type: "datetime"});
		laydate.render({elem: "#endTime", type: "datetime"});

		let roleTable = table.render({
			elem: "#tableId",
			url: okMock.api.listSell,
			limits: [10, 15, 20, 30],
			limit: 10,
			even: true,
			page: true,
			toolbar: "#toolbarTpl",
			method:"post",
			contentType: "application/json;charset=utf-8",
			headers: {
				token: okUtils.token,
			},
			request: {
				pageName: "pageNum",
				limitName: "pageSize"
			},
			parseData(res) {//格式化数据
				return {
					"code": res.code,
					"message": res.message,
					"count": res.data.result.total,
					"data": res.data.result.items
				}
			},
			response: {
				statusName: 'code', //数据状态的字段名称，默认：code, statusCode: 200 //成功的状态码，默认：0
				statusCode: 20000,//设置成功状态码为20000
				msgName: 'message', //状态信息的字段名称，默认：msg
				countName: 'count', //数据总数的字段名称，默认：count
				dataName: 'data' //数据列表的字段名称，默认：data
			},
			size: "lg",
			cols: [[
				{type: "checkbox"},
				{field: "sid", title: "销售号", width: 200, sort: true,},
				{field: "sellDescription", title: "销售描述", width: 400},
				{field: "operator", title: "操作员", width: 150},
				{field: "price", title: "价格", width: 100},
				{field: "is_debt", title: "是否欠款", width: 125, templet: "#statusTpl"},
				{field: "createTime", title: "创建时间", width: 250},
				{field: "modifiedTime", title: "最后修改时间", width: 250},
				{title: "操作", align: "center", templet: "#operationTpl"}
			]],
			done: function (res, curr, count) {
				console.log(res, curr, count);
			}
		});

		form.on("submit(search)", function (data) {
			roleTable.reload({
				where: data.field,
				page: {curr: 1}
			});
			console.log("0000")
			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "add":
					add();
					break;
				case "batchDel":
					batchDel();
					break;
			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "edit":
					edit(data.sid);
					break;
				case "del":
					del(data.sid);
					break;
			}
		});

		function add() {
			okLayer.open("添加销售", "sell-add.html", "90%", "90%", null, function () {
				roleTable.reload();
			})
		}

		function batchDel() {
			okLayer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				let idsStr = okUtils.tableBatchCheck(table,"sid");
				console.log(idsStr);
				if (idsStr) {
					okUtils.ajax("/pipe/sell/batchDel", "delete", {idsStr: idsStr}, true).done(function (response) {
						okUtils.tableSuccessMsg(response.message);
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function edit(id) {
			okLayer.open("编辑角色", "role-edit.html?id=" + id, "90%", "90%", null, function () {
				roleTable.reload();
			})
		}

		function del(id) {
			okLayer.confirm("确定要删除吗？", function () {
				okUtils.ajax("/pipe/sell?sid="+id, "delete",null, true).done(function (response) {
					okUtils.tableSuccessMsg(response.message);
				}).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
		<div class="layui-inline" lay-event="batchDel"><i class="layui-icon layui-icon-delete"></i></div>
	</div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="statusTpl">
	{{#  if(d.isDebt == 0){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">未赊账</span>
	{{#  } else if(d.isDebt == 1) { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">赊账</span>
	{{#  } }}
</script>

<script type="text/html" id="operationTpl">
	<a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
	<a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
